<template>
  <div>
    <div class="bookrack">
      <!-- 顶部功能栏 -->
      <div class="topbar">
        <span is-link @click="show = true" class="item">
          <i class="iconfont icon-liebiao"></i>
        </span>
        <span class="item">书架</span>
        <router-link to="/search" tag="span" class="item">
          <i class="iconfont icon-sousuo1"></i>
        </router-link>
      </div>

      <!-- 书籍区域 -->
      <div :class="[{ container1: list }, { container2: !list }]">
        <!-- 书本 -->
        <router-link
          :to="'/reading?id=' + item.id"
          class="books"
          v-for="item in bookList"
          :key="item.id"
        >
          <!-- 图片 -->
          <div class="img">
            <img v-lazy="item.cover" alt="" />
          </div>
          <!-- 文字 -->
          <div class="text">
            <p class="bookname">{{ item.title }}</p>
            <p class="writer">{{ item.author }}</p>
          </div>
        </router-link>
      </div>

      <!-- 弹出层 -->
      <van-popup v-model="show" position="bottom" :style="{ height: '46.07%' }">
        <div class="control">
          <div class="top">
            <!-- 子类 -->
            <div class="line line1"></div>
            <div class="line line2"></div>
            <div
              class="item"
              v-for="item in iconArr"
              :key="item.id"
              @click="changeView(item.key)"
            >
              <div class="btn">
                <span><i :class="['iconfont', item.icon]"></i></span>
                <span class="todothing">{{ item.text }}</span>
              </div>
            </div>
          </div>
          <div class="bottom">
            <span class="cancel" @click="show = false">取消</span>
          </div>
        </div>
      </van-popup>
    </div>
    <!-- 底部弹出 -->
    <van-popup v-model="showPrise" position="bottom" :style="{ height: '50%' }">
      <!-- 打赏 -->
      <div class="dashang">
        <span class="pay">{{ changeWord }}?</span>
        <div class="img">
          <img :src="changeImg" width="200" alt="" />
        </div>
      </div>
    </van-popup>
    <!-- 分享 -->
    <van-share-sheet
  v-model="showShare"
  title="立即分享给好友"
  :options="options"
/>
    <!-- 底部导航 -->
    <footernav />
  </div>
</template>

<script>
import { Toast } from "vant";
import footernav from "../components/base/footernav.vue";
import { mapState } from "vuex";
export default {
  data() {
    return {
      show: false, //显示弹出层
      list: true, //控制小说摆放是否为列表样式
      showPrise: false, //打赏
      showShare:false, //分享
      img: require("../assets/img/book_cover.jpg"),
      changeWord: "给点",
      changeImg: require("../assets/img/vx_pay.jpg"), //能更改的图片
      iconArr: [
        { id: 1, icon: "icon-qiehuanshitu1", key: "qiehuan", text: "切换" },
        { id: 2, icon: "icon-dashang", key: "dashang", text: "打赏" },
        { id: 3, icon: "icon-weixin", key: "lianxi", text: "联系" },
        { id: 4, icon: "icon-tuijian", key: "dianzan", text: "点赞" },
        { id: 5, icon: "icon-wifi", key: "chuanshu", text: "传书" },
        { id: 6, icon: "icon-fenxiang_2", key: "fenxiang", text: "分享" },
      ],
      // 分享
       options: [
        { name: '微信', icon: 'wechat' },
        { name: '微博', icon: 'weibo' },
        { name: '复制链接', icon: 'link' },
        { name: '分享海报', icon: 'poster' },
        { name: '二维码', icon: 'qrcode' },
      ],
    };
  },
  methods: {
    // 点击弹出的内容切换视图
    changeView(key) {
      if (key == "qiehuan") {
        this.list = !this.list;
        this.show = false;
        setTimeout(() => {
          if (this.list) {
            Toast("已切换为列表模式");
          } else {
            Toast("已切换为宫格模式");
          }
        }, 100);
      }
      // 打赏
      else if (key == "dashang") {
        this.changeWord = "给点";
        this.changeImg = require("../assets/img/vx_pay.jpg");
        this.showPrise = true;
      }
      // 联系
      else if (key == "lianxi") {
        this.changeWord = "加好友";
        this.changeImg = require("../assets/img/add_vx.jpg");
        this.showPrise = true;
      }
      // 点赞
      else if (key == "dianzan") {
        Toast("已经给作者一个大大的赞了");
      }
      // 传书
      else if (key == "chuanshu") {
        Toast("没有连到WiFi");
      }
      // 分享
      else if (key == "fenxiang") {
        console.log("分享");
        this.showShare = true;
      }
    },
  },
  computed: {
    // 应用vuex中的数据
    ...mapState(["bookList"]),
  },
  components: {
    footernav,
  },
};
</script>

<style lang='less'>
.dashang {
  width: 100%;
  height: 100%;
  position: relative;
  text-align: center;
  .pay {
    font-size: 18px;
    height: 60px;
    line-height: 60px;
  }
  .img {
    position: absolute;
    margin: auto;
    bottom: 0;
    left: 0;
    right: 0;

    width: 200px;
    img {
      width: 200px;
    }
  }
}
.bookrack {
  padding: 40px 0px 50px 0px;
  .van-popup {
    background-color: transparent;
    width: calc(100% - 20px);
    padding: 0 10px;
    position: absolute;
  }
  .control {
    height: 100%;
    display: flex;
    flex-direction: column;
    .top {
      background-color: #fff;
      display: flex;
      flex-wrap: wrap;
      border-radius: 8px;
      position: relative;
      .line {
        width: 0.5px;
        height: 190px;
        background-color: #e9e9e9;
        position: absolute;
        margin: auto;
        top: 0;
        bottom: 0;
      }
      .line1 {
        left: 33.33333%;
      }
      .line2 {
        left: calc(33.33333% * 2);
      }
      &::before {
        content: "";
        display: block;
        height: 1px;
        width: calc(100% - 40px);
        background-color: #e9e9e9;
        position: absolute;
        top: 50%;
        left: 0;
        right: 0;
        margin: auto;
        margin-top: -1px;
      }
      .item {
        flex: 33.333333%;
        height: 120px;
        display: flex;
        align-items: center;
        justify-content: center;
        .btn {
          span {
            display: block;
            text-align: center;
            color: #797979;
            .iconfont {
              font-size: 24px;
            }
          }
          .todothing {
            margin-top: 10px;
            font-size: 14px;
            color: #565656;
          }
        }
      }
    }
    .bottom {
      flex: 1;
      display: flex;
      align-items: center;
      .cancel {
        display: block;
        width: 100%;
        height: 40px;
        background-color: #fff;
        text-align: center;
        line-height: 40px;
        color: #595959;
        border-radius: 8px;
      }
    }
  }
  .topbar {
    max-width: 750px;
    min-width: 320px;
    margin: 0 auto;
    height: 40px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background-color: #52b594;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    .item {
      color: #fff;
      font-weight: 550;
      i {
        font-size: 20px;
        font-weight: normal;
      }
    }
  }
  // 第一套样式
  .container1 {
    .books {
      margin-left: 13px;
      padding: 10px 0;
      border-bottom: 1px solid #f1f1f1;
      display: flex;
      height: 96px;
      // 最后一本书没有下边框
      &:last-child {
        border: none;
      }
      .img {
        flex: 0 0 74px;
        height: 96px;
        margin-right: 3.7333%;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .text {
        flex: 1;
        p {
          display: flex;
          height: 50%;
          align-items: center;
          font-size: 14px;
          color: #818181;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .writer {
          font-size: 12px;
          color: #8e8e8e;
        }
      }
    }
  }
  // 第二套样式
  .container2 {
    display: flex;
    flex-wrap: wrap;
    margin: 0 auto;
    .books {
      flex: 0 0 33.33333%;
      padding: 10px 0;
      text-align: center;
      .img {
        display: inline-block;
        width: 74px;
        height: 96px;
        margin-right: 3.7333%;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .text {
        p {
          display: inline-block;
          width: 74px;
          height: 50%;
          align-items: center;
          font-size: 14px;
          color: #818181;
          text-align: left;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .writer {
          font-size: 12px;
          color: #8e8e8e;
        }
      }
    }
  }
}
</style>